package com.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Result;
import com.demo.pojo.dto.CourseListDTO;
import com.demo.pojo.entity.Course;
import com.demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程控制器
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    
    @Autowired
    private CourseService courseService;
    
    /**
     * 分页查询课程列表
     * @param pageNum 当前页
     * @param pageSize 每页大小
     * @param courseNo 课程编号（可选）
     * @param courseName 课程名称（可选）
     * @param courseType 课程类别（可选）
     * @param dept 院系（可选）
     * @param teacherId 教师ID（可选）
     * @return 分页结果
     */
    @GetMapping("/listPage")
    public Result<IPage<CourseListDTO>> getCourseList(
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "courseNo", required = false) Long courseNo,
            @RequestParam(name = "courseName", required = false) String courseName,
            @RequestParam(name = "courseType", required = false) String courseType,
            @RequestParam(name = "dept", required = false) String dept,
            @RequestParam(name = "teacherId", required = false) Long teacherId) {
        Page<Course> page = new Page<>(pageNum, pageSize);
        IPage<CourseListDTO> result = courseService.getCourseList(page, courseNo, courseName, courseType, dept, teacherId);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询课程
     * @param id 课程ID
     * @return 课程信息
     */
    @GetMapping("/select")
    public Result<Course> getCourseById(@RequestParam Long id) {
        Course course = courseService.getCourseById(id);
        if (course == null) {
            return Result.error("课程不存在");
        }
        return Result.success(course);
    }
    
    /**
     * 根据课程编号查询课程
     * @param courseNo 课程编号
     * @return 课程信息
     */
    @GetMapping("/no/{courseNo}")
    public Result<Course> getCourseByNo(@PathVariable Long courseNo) {
        Course course = courseService.getCourseByNo(courseNo);
        if (course == null) {
            return Result.error("课程不存在");
        }
        return Result.success(course);
    }
    
    /**
     * 添加课程
     * @param course 课程信息
     * @return 操作结果
     */
    @PostMapping("/save")
    public Result<String> addCourse(@RequestBody Course course) {
        try {
            boolean success = courseService.addCourse(course);
            if (success) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新课程信息
     * @param course 课程信息
     * @return 操作结果
     */
    @PutMapping("/update")
    public Result<String> updateCourse(@RequestBody Course course) {
        try {
            boolean success = courseService.updateCourse(course);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除课程
     * @param id 课程ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Result<String> deleteCourse(@RequestParam Long id) {
        boolean success = courseService.deleteCourse(id);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
    
    /**
     * 批量删除课程
     * @param ids 课程ID列表
     * @return 操作结果
     */
    @DeleteMapping("/batch")
    public Result<String> deleteCourses(@RequestBody List<Long> ids) {
        boolean success = courseService.deleteCourses(ids);
        if (success) {
            return Result.success("批量删除成功");
        } else {
            return Result.error("批量删除失败");
        }
    }
    
    /**
     * 获取所有课程列表
     * @return 课程列表
     */
    @GetMapping("/all")
    public Result<List<Course>> getAllCourses() {
        List<Course> courses = courseService.getAllCourses();
        return Result.success(courses);
    }
}

























